package com.chenyuxin.ilp.controller.file;

import com.chenyuxin.ilp.model.dto.file.CreateFolderDto;
import com.chenyuxin.ilp.model.dto.file.RenameFolderDto;
import com.chenyuxin.ilp.model.pojo.Result;
import com.chenyuxin.ilp.service.file.FolderService;
import com.chenyuxin.ilp.util.SecurityUtil;
import jakarta.validation.Valid;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/file/folder")
public class FolderController {
    @Autowired
    private FolderService folderService;
    // 创建文件夹
    @PostMapping("/create")
    public Result createFolder(@RequestBody @Valid CreateFolderDto createFolderDto){
        String userId = SecurityUtil.getUserId();
        return folderService.createFolder(userId,createFolderDto);
    }
    // 删除文件夹
    @DeleteMapping("/delete/{folderid}")
    public Result deleteFolder(@PathVariable("folderid") Long folderId){
        String userId = SecurityUtil.getUserId();
        return folderService.deleteFolder(userId,folderId);
    }
    // 查询用户根文件夹
    @GetMapping("/root")
    public Result selectRootFolder(){
        String userId = SecurityUtil.getUserId();
        return folderService.selectRootFolder(userId);
    }
    // 重命名文件夹
    @PostMapping("/rename")
    public Result renameFolder(@RequestBody RenameFolderDto renameFolderDto){
        String userId = SecurityUtil.getUserId();
        return folderService.renameFolder(userId,renameFolderDto);
    }
    // 修改文件夹权限
    @PostMapping("/authority")
    public Result changeFolderOpen(@RequestParam("folderid") Long folderId,
                                 @RequestParam("authority")@NotNull("authority参数为空") Integer isOpen){
        String userId = SecurityUtil.getUserId();
        return folderService.changeFolderOpen(folderId,isOpen,userId);
    }
}
